package samsung.sds.groupManagement.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import samsung.sds.groupManagement.dao.GroupMapper;
import samsung.sds.groupManagement.dao.GroupUserMapper;
import samsung.sds.groupManagement.model.GroupEntity;
import samsung.sds.groupManagement.service.GroupManagementService;
import samsung.sds.system.common.baseQuery.AbstractBaseService;
import samsung.sds.system.common.baseQuery.BaseMapper;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.userManagement.model.UserEntity;

/**
 * 
 * @author wch.wu&zhukui.shi
 * @createDate 2016-01-07
 *
 * 用户组管理ServiceImpl
 */
@Service
public class GroupManagementServiceImpl extends AbstractBaseService<GroupEntity> implements GroupManagementService{
	
	@Autowired
	private GroupMapper groupMapper;

	@Autowired
	private GroupUserMapper groupUserMapper;

	@Override
	protected BaseMapper<GroupEntity> getMapper() {
		return groupMapper;
	}
	
	/**
	 * 查询所有组
	 * @param userName
	 * @param map
	 * @return
	 */
	public List<GroupEntity> selectUserGroupList(String  createUser, Map<String, Object> map) {
		 
		map.put("createUser", createUser);
	    QueryParam  queryParam= new QueryParam();
	    queryParam.setQueryCondition(map);
		List<GroupEntity> groupList = groupMapper.queryList( queryParam );
		for (GroupEntity groupEntity : groupList) {
			String groupId = groupEntity.getGroupId();
			List<UserEntity> userEntities = new ArrayList<UserEntity>();
			userEntities = groupUserMapper.selectSYS_USER( groupId );
			groupEntity.setUserEntities(userEntities);
		}
		
		return groupList;
	}
	
	/**
	 * 用户创建组
	 * @param groupEntity
	 * @return
	 */
	 public int  insertEntity(GroupEntity groupEntity){
		 
		    return groupMapper.insertEntity(groupEntity);
	 }
	 
	/**
	 * 用户更新组
	 * @param groupEntity
	 * @return
	 */
	 public int  updateEntity(GroupEntity groupEntity){
		 
		    return groupMapper.updateEntity(groupEntity);
	 }
	 
	/**
	 * 删除用户组及其相关数据（可以同时删除多个组）
	 * @param groupEntity
	 * @return
	 */
    public int deleteUserGroupsByGroupIds(List<String> groupIds){
		
		// 删除组下的所有用户
		groupUserMapper.deleteTF_F_GROUP_USER01( groupIds );
		// 删除用户组
		int count = groupMapper.deleteTF_F_GROUP(groupIds);
		
		return count;
	}

	/**
	 * 查询组是否拥有用户
	 * @param groupId
	 * @return
	 */
	@Override
	public int selectGroupUsersCount(String groupId) {
		return groupMapper.selectTF_F_GROUP_USER(groupId);
	}
	
}

